package com.qipay.api.widthdraw.controller.v1;


import com.qipay.api.widthdraw.controller.v1.vo.CpBalanceQueryVo;
import com.qipay.api.widthdraw.controller.v1.vo.CpPayUnionTradeQueryVo;
import com.qipay.api.widthdraw.controller.v1.vo.CpWithdrawVo;
import com.qipay.pay.entity.CpPayUnionTradeQueryRequest;
import com.qipay.scalar.type.ApiStatusEnum;
import com.qipay.security.jwt.JWTUser;
import com.qipay.utils.IPUtils;
import com.qipay.utils.ResultUtils;
import com.qipay.utils.XmlUtils;
import com.qipay.withdraw.entity.CpBalanceQueryRequest;
import com.qipay.withdraw.service.WithdrawService;
import com.qipay.withdraw.service.entity.CpWithdrawRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@RestController
@RequestMapping("/sig/v1/withdraw")
public class WithdrawController {

    @Value("${api.response.sign.name}")
    String apiResponseSignName;

    @Value("${api.response.key.name}")
    String apiResponseKeyName;

    @Autowired
    WithdrawService withdrawService;

    @RequestMapping(value = "/native", method = RequestMethod.POST, produces = {"application/xml;charset=UTF-8"})
    private String withdraw(
            HttpServletRequest request,
            HttpServletResponse response,
            @AuthenticationPrincipal JWTUser user
    ) throws Exception {
        Map<String, Object> params = (Map<String, Object>) request.getAttribute("params");
        CpWithdrawVo vo = (CpWithdrawVo) XmlUtils.mapToBean(
                params, CpWithdrawVo.class);
        CpWithdrawRequest bean = new CpWithdrawRequest();
        BeanUtils.copyProperties(vo, bean);

        String ip = IPUtils.getIpAddr(request);
        bean.setMchCreateIp(ip);

        Map<String, Object> result = withdrawService.withdraw(bean, user);

        Assert.notNull(result,
                ResultUtils.buildErrorMsg(ApiStatusEnum.SERVICE_BUSY, ApiStatusEnum.SERVICE_BUSY.getInfo()));

        result = ResultUtils.wrapResult(ApiStatusEnum.OK.getCode().toString(), null, result);
        result = ResultUtils.signResult(result, user.getSecret(),apiResponseSignName, apiResponseKeyName);
        return XmlUtils.toString(result, "xml");

    }

    /**
     * 余额查询
     * @param request
     * @param response
     * @param user
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/balance/query", method = RequestMethod.POST, produces = {"application/xml;charset=UTF-8"})
    @ResponseBody
    private String queryBalance(
            HttpServletRequest request,
            HttpServletResponse response,
            @AuthenticationPrincipal JWTUser user
    ) throws Exception {
        Map<String, Object> params = (Map<String, Object>) request.getAttribute("params");
        CpBalanceQueryVo vo = (CpBalanceQueryVo) XmlUtils.mapToBean(
                params, CpBalanceQueryVo.class);
        CpBalanceQueryRequest bean = new CpBalanceQueryRequest();
        BeanUtils.copyProperties(vo, bean);

        Map<String, Object> result = withdrawService.queryBalance(bean, user);

        Assert.notNull(result,
                ResultUtils.buildErrorMsg(ApiStatusEnum.SERVICE_BUSY, ApiStatusEnum.SERVICE_BUSY.getInfo()));

        result = ResultUtils.wrapResult(ApiStatusEnum.OK.getCode().toString(), null, result);
        result = ResultUtils.signResult(result, user.getSecret(),apiResponseSignName, apiResponseKeyName);
        return XmlUtils.toString(result, "xml");
    }

    /**
     * 交易状态查询
     * @param request
     * @param response
     * @param user
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/trade/query", method = RequestMethod.POST, produces = {"application/xml;charset=UTF-8"})
    private String tradeQuery(
            HttpServletRequest request,
            HttpServletResponse response,
            @AuthenticationPrincipal JWTUser user
    ) throws Exception {
        Map<String, Object> params = (Map<String, Object>) request.getAttribute("params");
        CpPayUnionTradeQueryVo vo = (CpPayUnionTradeQueryVo) XmlUtils.mapToBean(
                params, CpPayUnionTradeQueryVo.class);
        CpPayUnionTradeQueryRequest bean = new CpPayUnionTradeQueryRequest();
        BeanUtils.copyProperties(vo, bean);

        Map<String, Object> result = withdrawService.withdrawQuery(bean, user);

        Assert.notNull(result,
                ResultUtils.buildErrorMsg(ApiStatusEnum.SERVICE_BUSY, ApiStatusEnum.SERVICE_BUSY.getInfo()));

        result = ResultUtils.wrapResult(ApiStatusEnum.OK.getCode().toString(), null, result);
        result = ResultUtils.signResult(result, user.getSecret(),apiResponseSignName, apiResponseKeyName);
        return XmlUtils.toString(result, "xml");
    }

}
